import {
  GetMenuList
} from 'api'
// import {
//   menu
// } from 'src/api/menu.js'
import {
  DeepCopy
} from 'util'
const menuStore = {
  state: {
    menuData: [],
    menuArr: [],
    breadcrumbArr: []
  },
  actions: {
    selectMainMenu ({commit, state}, {id}) {
      commit('changeMainMenu', {id})
    },
    selectSubMenu ({commit, state}, id, path) {

    },
    setMenuTreeModel ({dispatch, commit, state}, callback) {
      dispatch('getMenuTree').then(({menuRoot, menuArr}) => {
        state.menuData = menuRoot
        state.menuArr = menuArr
        callback && callback()
      })
    },
    getMenuTree ({commit, state}) {
      return new Promise((resolve, reject) => {
        GetMenuList({
          test: 1
        }).then((res) => {
          // res.data = menu
          if (res.data.code === 100) {
            let menuArr = DeepCopy(res.data.data)
            let menuRoot = []
            menuArr.forEach((item, index) => {
              if (!item.presId) {
                menuRoot.push(item)
              }
            })
            menuRoot.forEach((item, index) => {
              item['children'] = getMenuTree(menuArr, item)
            })
            resolve({
              menuRoot,
              menuArr: res.data.data
            })
          }
        })
      })
    },
    setBreadcrumb ({commit, state}, breadcrumbArr) {
      state.breadcrumbArr = breadcrumbArr
      // let breadcrumbData = []
      // let menuArr = state.menuArr

      // if (!menuArr) {
      //   return
      // }
      // breadcrumbArr.forEach((breadcrumbArrItem, index) => {
      //   let route = menuArr.find((menuArrItem) => {
      //     return menuArrItem.labelId === breadcrumbArrItem
      //   })
      //   let breadcrumbTarget = {
      //     butRule: route.butRule,
      //     name: route.resName
      //   }
      //   breadcrumbData.push(breadcrumbTarget)
      // })
      // console.log(breadcrumbData)
    }
  },
  mutations: {
    curMainMenuItem (state, id) {
      state.curMainMenuName = id
    }
  },
  getters: {
    mainMenuData: state => state.menuData,
    defaultItem: (state) => {
      return state.curMainMenuName
    },
    subMenu: (state, getters) => {
      let {curMainMenuName, menuData} = state
      let subMenuTree = []
      let curMainMenuItem = menuData.filter((item, index) => {
        return item.resName === curMainMenuName
      })
      if (curMainMenuItem.length > 0) {
        subMenuTree = curMainMenuItem[0].children
      }
      return subMenuTree
    },
    breadcrumbData: (state) => {
      let {breadcrumbArr} = state
      let breadcrumbData = []
      let menuArr = state.menuArr
      if (menuArr.length === 0) {
        return []
      }
      breadcrumbArr.forEach((breadcrumbArrItem, index) => {
        let route = menuArr.find((menuArrItem) => {
          return menuArrItem.labelId === breadcrumbArrItem
        })
        let breadcrumbTarget = {
          butRule: route.butRule,
          resName: route.resName
        }
        breadcrumbData.push(breadcrumbTarget)
      })
      return breadcrumbData
    }
  }
}

// 获取菜单树
function getMenuTree (data, root) {
  let idTxt = 'resId'
  let pidTxt = 'presId'
  let pushKey = 'children'

  function getNode (rootItem) {
    let node = []
    data.forEach((item, index) => {
      if (item[pidTxt] === rootItem[idTxt]) {
        data[index][pushKey] = getNode(data[index])
        node.push(data[index])
      }
    })
    return node
  }
  return getNode(root)
}

export default menuStore
